package com.wangzh.concurrent.lock;

/*
 * 线程就是一个单独的资源，没有任何附属的操作
 * */
public class LockTest {
    public static void main(String[] args) {

        Ticket ticket = new Ticket();

        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "A1").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "A2").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "A3").start();

    }
}

//资源类oop
class Ticket {
    private int number = 50;

    //    卖票的方式
    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + number-- + ";剩余:" + number);

        }
    }
}